package com.webdemo.servlet;

import java.io.IOException;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.webdemo.dao.Dao;
import com.webdemo.entity.User;

/**
 * 用于注册用户中转
 */
@SuppressWarnings("serial")
public class EnrollServlet extends HttpServlet {
	String passWord = "";
	String userName = "";

	/*
	 * 转到doPost()处理
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	/**
	 * 处理注册表单提交的用户信息
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		userName = ((String) request.getParameter("username"));
		passWord = ((String) request.getParameter("password"));

		try {
			if (Dao.query(userName) != null&&!"".equals(Dao.query(userName).getUserName())) {
				// 用户输入的用户名如果存在 则跳转其他页面提示用户 该用户名已存在
				request.getRequestDispatcher("newenroll.jsp").forward(request,
						response);
			} else {

				if (userName != null && passWord != null
						&& !"".equals(userName.trim()) && !"".equals(passWord)) {
					User user = new User(userName, passWord);
					Dao.addUser(user);
				}
				// 注册成功之后跳转到记事本页面
				request.getRequestDispatcher("Note.jsp").forward(request,
						response);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
